<template>
  <el-container style="padding: 10px">
    <el-aside>
      <el-table
        size="mini"
        :data="roleList">
        <el-table-column
          prop="roleType"
          label="角色名">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色类型"
          :formatter="roleTypeFormatter">
        </el-table-column>
      </el-table>
    </el-aside>
    <el-main>
      <el-form size="mini">
        <el-form-item label="角色名">
          <el-input v-model="formData.roleType"></el-input>
        </el-form-item>
        <el-form-item label="角色类型">
          <el-select v-model="formData.roleName" placeholder="选择角色类型">
            <el-option value="DOC-NU">医护人员</el-option>
            <el-option value="ADMIN">管理员</el-option>
            <el-option value="SERVANT">服务人员</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRoleClick">新增</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
  export default {
    name: "index",
    data: function () {
      return {
        formData: {
          roleType: null,
          roleName: null,
        },
        roleList: []
      }
    },
    created() {
      this.getRoleList();
    },
    methods: {
      roleTypeFormatter(val) {
        if (val.roleName === 'ADMIN') {
          return "管理员";
        } else if (val.roleName === 'DOC-NU') {
          return "医护人员";
        }
        return "服务人员"
      },
      getRoleList() {
        this.$api.roleList().then(res => {
          this.roleList = res;
        })
      },
      handleRoleClick() {
        this.$api.saveRole(this.formData).then(res => {
          this.getRoleList();
        })
      }
    }
  }
</script>

<style scoped>

</style>
